﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BsPortalModel;
using EntitiesModel;

namespace ITMan
{
    public partial class FormOrgs : Form
    {
        public FormOrgs()
        {
            InitializeComponent();
        }

        // <summary>
        /// TreeView数据绑定
        /// </summary>
        /// <param name搜索="list"></param>
        protected void CreateTreeView()
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Text = @"上海百胜软件有限公司";
            rootNode.Tag = "root";
            treeViewOrgs.Nodes.Add(rootNode);

            using (var bsportal = new BsPortalModel.PortalEntities())
            {
                IList<Organization_> iOrgs = (from i in bsportal.Organization_s
                                                  where i.IsDelete==1
                                                  orderby i.SortId
                                                  select i).ToList();
                //for (int i = 0; i < iOrgs.Count; i++)
                //{
                //    if (iOrgs[i].ParentOrganizationId == 0)
                //    {
                //        TreeNode node = new TreeNode();
                //        node.Text = iOrgs[i].Name;
                //        node.Name = iOrgs[i].OrganizationId.ToString();
                //        rootNode.Nodes.Add(node);
                //        node.Expand();
                //
                //        CreateChildNodes(node, iOrgs, iOrgs[i].Name, iOrgs[i].OrganizationId);
                //    }
                //}
                
                foreach (var organization_ in iOrgs)
                {
                    if (organization_.ParentOrganizationId == 0)
                    {
                        TreeNode node = new TreeNode();
                        node.Text = organization_.Name;
                        node.Name = organization_.OrganizationId.ToString();
                        rootNode.Nodes.Add(node);
                        rootNode.Expand();
                       
                        CreateChildNodes(node, iOrgs, organization_.Name, organization_.OrganizationId);
                    }
                }
            }

        }

  
        public void CreateChildNodes(TreeNode treenode, IList<Organization_> orgs, string text, long tag)
        {
            IList<Organization_> subOrg = (from i in orgs
                where i.ParentOrganizationId == tag
                orderby i.SortId
                select i).ToList();

            foreach (Organization_ org in subOrg)
            {
                TreeNode childnode=new TreeNode();
                childnode.Text = org.Name;
                childnode.Name = org.OrganizationId.ToString();
                treenode.Nodes.Add(childnode);
                CreateChildNodes(childnode,orgs,org.Name,org.OrganizationId);
            }
        }

        private void treeViewOrgs_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void treeViewOrgs_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void treeViewOrgs_ItemDrag(object sender, ItemDragEventArgs e)
        {
            this.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        private void FormOrgs_Load(object sender, EventArgs e)
        {
            treeViewOrgs.Nodes.Clear();
            CreateTreeView();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            FormOrgs_Load(sender,e);
        }
    }
}
